package com.example.kermit.zhpaper.http;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

/**
 * Created by Kermit on 15-7-25.
 * e-mail : wk19951231@163.com
 */
public class MySingleRequestQueue {

    private static MySingleRequestQueue mInstance;
    private RequestQueue requestQueue;
    private ImageLoader imageLoader;
    private static Context context;

    public MySingleRequestQueue(Context context){
        this.context = context;
        requestQueue = getRequestQueue();
        imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {

            int maxSize = 10 * 1024 * 1024;
            private final LruCache<String, Bitmap> cache =
                    new LruCache<String, Bitmap>(maxSize);

            @Override
            public Bitmap getBitmap(String url){
                return cache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                cache.put(url, bitmap);
            }
        });
    }

    public static synchronized MySingleRequestQueue getInstance(Context context){
        if (mInstance == null){
            mInstance = new MySingleRequestQueue(context);
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (requestQueue == null){
            requestQueue = Volley.newRequestQueue(context.getApplicationContext());
        }
        return requestQueue;
    }

    public <T> void addtoRequestQueue(Request<T> req){
        getRequestQueue().add(req);
    }

    public ImageLoader getImageLoader(){
        return imageLoader;
    }
}
